package com.example.yunifang;

import java.util.ArrayList;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.RadioButton;

import com.example.yunifang.adapter.QuanBuAdapter;
import com.example.yunifang.bean.QuanBuBean;
import com.example.yunifang.bean.QuanBuBean.Data;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class QuanBuActivity extends Activity {

	private RadioButton quanbu_back_radio;
	private GridView quanbu_gd;
	private ArrayList<Data> data=new ArrayList<Data>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_quan_bu);
		init();
		quanbu_back_radio.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				overridePendingTransition(R.anim.infragment, R.anim.outfragment);
			}
		});
		getJson();
	}

	public void init() {
		quanbu_back_radio = (RadioButton) findViewById(R.id.quanbu_back_radio);
		quanbu_gd=(GridView)findViewById(R.id.quanbu_gd);
	}

	private void getJson() {
		// TODO Auto-generated method stub
		HttpUtils utils = new HttpUtils();
		utils.send(
				HttpMethod.GET,
				"http://m.yunifang.com/yunifang/mobile/goods/getall?random=87749&encode=ac6bd45b8f50b626a6843b294af8fed5",
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						String json=arg0.result;
						Gson gson=new Gson();
						QuanBuBean bean=gson.fromJson(json, QuanBuBean.class);
						initData(bean);
					}

					
				});
	}
	public void initData(final QuanBuBean bean) {
		data.clear();
		data.addAll(bean.data);
		QuanBuAdapter adapter=new QuanBuAdapter(data, this);
		quanbu_gd.setAdapter(adapter);
		quanbu_gd.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String id=bean.data.get(arg2).id;
				Intent intent=new Intent(QuanBuActivity.this,XiangQingActivity.class);
				int ids=Integer.parseInt(id);
				intent.putExtra("id", ids);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.quan_bu, menu);
		return true;
	}

}
